{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bf1cba95",
   "metadata": {},
   "source": [
    "| [01_base/02_列表类型_list.ipynb](https://github.com/shibing624/python-tutorial/blob/master/01_base/02_列表类型_list.ipynb)  | Python列表类型  |[Open In Colab](https://colab.research.google.com/github/shibing624/python-tutorial/blob/master/01_base/02_列表类型_list.ipynb) |\n",
    "\n",
    "\n",
    "# 列表\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b07f0849",
   "metadata": {},
   "source": [
    "介绍列表的方法及示例演示其使用，包括：长度、修改列表、取值、排序\n",
    "\n",
    "## 创建列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e9fed70",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "empty_list = list()\n",
    "print(empty_list)  # 同 empty_list = []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb674e62",
   "metadata": {},
   "source": [
    "查看列表长度："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aba5435b",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# len 查看列表长度\n",
    "a = [1, 2, 3]\n",
    "b = [2, 3, 'hello']\n",
    "c = a + b\n",
    "print(c)  # [1, 2, 3, 2, 3, u'hello']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55459d24",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "len(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98552f37",
   "metadata": {},
   "source": [
    "Python字符串可以和列表可以方便扩展："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39fbc377",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "d = b * 2\n",
    "print(d)  # [2, 3, u'hello', 2, 3, u'hello']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a0cea46",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "d[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2227df58",
   "metadata": {},
   "source": [
    "## 修改列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f7dec57",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "print(a)\n",
    "a[0] = 100\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1caa2d2",
   "metadata": {},
   "source": [
    "这种赋值也适用于分片，例如，将列表的第2，3两个元素换掉："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "167516f8",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a[1:3] = [200, 300]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62a26d17",
   "metadata": {},
   "source": [
    "事实上，对于连续的分片（即步长为 1 ），Python采用的是整段替换的方法，两者的元素个数并不需要相同，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8c343fe",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 例如，将 [11,12] 替换为 [1,2,3,4]：\n",
    "a = [10, 11, 12, 13, 14]\n",
    "a[1:3] = [1, 2, 3, 4]\n",
    "print(a)  # [10, 1, 2, 3, 4, 13, 14]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2aac6dfd",
   "metadata": {},
   "source": [
    "用这种方法来删除列表中一个连续的分片："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c34a1e70",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a = [10, 1, 2, 11, 12]\n",
    "print(a[1:3])\n",
    "a[1:3] = []\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc0dcc69",
   "metadata": {},
   "source": [
    "对于不连续（间隔step不为1）的片段进行修改时，两者的元素数目必须一致："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c00c8082",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a = [10, 11, 12, 13, 14]\n",
    "a[::2] = [1, 2, 3]\n",
    "print(a)  # [1, 11, 2, 13, 3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c91030a0",
   "metadata": {},
   "source": [
    "Python提供了删除列表中元素的方法 'del':"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ca7e325",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a = [100, 'a', 'b', 200]\n",
    "del a[0]\n",
    "print(a)  # [u'a', u'b', 200]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8096ae67",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 删除间隔的元素：\n",
    "a = ['a', 1, 'b', 2, 'c']\n",
    "del a[::2]\n",
    "print(a)  # [1, 2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "863f3ae0",
   "metadata": {},
   "source": [
    "用 in 来看某个元素是否在某个序列（不仅仅是列表）中，\n",
    "用not in来判断是否不在某个序列中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6731b3a5",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a = [1, 2, 3, 4, 5]\n",
    "print(1 in a)\n",
    "print(1 not in a)\n",
    "\n",
    "# 也可以作用于字符串：\n",
    "s = 'hello world'\n",
    "print(\"'he' in s : \", 'he' in s)  # True\n",
    "print(\"'world' not in s : \", 'world' not in s)  # False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b522376d",
   "metadata": {},
   "source": [
    "列表中可以包含各种对象，甚至可以包含列表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "147a08fe",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a = [1, 2, 'six', [3, 4]]\n",
    "print(a[3])  # [3,4]\n",
    "# a[3]是列表，可以对它再进行索引：\n",
    "print(a[3][1])  # 4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e47a41b5",
   "metadata": {},
   "source": [
    "## 列表方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97bb41f3",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 列表中某个元素个数\n",
    "a = [1, 1, 2, 3, 4, 5]\n",
    "print(len(a))  # 总个数：6\n",
    "# 元素1出现的个数\n",
    "print(a.count(1))  # 2\n",
    "# l.index(ob) 返回列表中元素 ob 第一次出现的索引位置，如果 ob 不在 l 中会报错。\n",
    "print(a.index(1))  # 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98db6f96",
   "metadata": {},
   "source": [
    "### 列表添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae36d523",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 向列表添加单个元素\n",
    "# a.append(ob) 将元素 ob 添加到列表 a 的最后。\n",
    "a = [1, 1, 2, 3, 4, 5]\n",
    "a.append(10)\n",
    "print(a)  # [1, 1, 2, 3, 4, 5, 10]\n",
    "\n",
    "# append每次只添加一个元素，并不会因为这个元素是序列而将其展开：\n",
    "a.append([11, 12])\n",
    "print(a)  # [1, 1, 2, 3, 4, 5, 10, [11, 12]]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e5496f7",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 向列表添加序列\n",
    "# l.extend(lst) 将序列 lst 的元素依次添加到列表 l 的最后，作用相当于 l += lst。\n",
    "a = [1, 2, 3, 4]\n",
    "a.extend([6, 7, 1])\n",
    "print(a)  # [1, 2, 3, 4, 6, 7, 1]\n",
    "\n",
    "# 插入元素\n",
    "# l.insert(idx, ob) 在索引 idx 处插入 ob ，之后的元素依次后移。\n",
    "a = [1, 2, 3, 4]\n",
    "# 在索引 3 插入 'a'\n",
    "a.insert(3, 'a')\n",
    "print(a)  # [1, 2, 3, u'a', 4]\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7365b109",
   "metadata": {},
   "source": [
    "### 移除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0108a322",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# l.remove(ob) 会将列表中第一个出现的 ob 删除，如果 ob 不在 l 中会报错。\n",
    "a = [1, 1, 2, 3, 4]\n",
    "# 移除第一个1\n",
    "a.remove(1)\n",
    "print(a)  # [1, 2, 3, 4]\n",
    "\n",
    "# 弹出元素\n",
    "# l.pop(idx) 会将索引 idx 处的元素删除，并返回这个元素。\n",
    "a = [1, 2, 3, 4]\n",
    "b = a.pop(0)  # 1\n",
    "print('pop:', b, ' ;result:', a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c51c05f",
   "metadata": {},
   "source": [
    "### 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcbe9ce0",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# l.sort() 会将列表中的元素按照一定的规则排序：\n",
    "a = [10, 1, 11, 13, 11, 2]\n",
    "a.sort()\n",
    "print(a)  # [1, 2, 10, 11, 11, 13]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b566aa2",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 如果不想改变原来列表中的值，可以使用 sorted 函数：\n",
    "a = [10, 1, 11, 13, 11, 2]\n",
    "b = sorted(a)\n",
    "print(a)  # [10, 1, 11, 13, 11, 2]\n",
    "print(b)  # [1, 2, 10, 11, 11, 13]\n",
    "\n",
    "# 列表反向\n",
    "# list.reverse() 会将列表中的元素从后向前排列。\n",
    "a = [1, 2, 3, 4, 5, 6]\n",
    "a.reverse()\n",
    "print(a)  # [6, 5, 4, 3, 2, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "daec83cb",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 如果不想改变原来列表中的值，可以使用这样的方法：\n",
    "a = [1, 2, 3, 4, 5, 6]\n",
    "b = a[::-1]\n",
    "print(a)\n",
    "print(b)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "058430fa",
   "metadata": {},
   "source": [
    "\n",
    "如果不清楚用法，可以查看帮助：a.sort? 或者 help(a.sort)\n",
    "在 Python Console 输入：a=[1,2,3];a.sort?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e48831b",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a.sort?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ac459d2",
   "metadata": {},
   "source": [
    "显示帮助："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25bc0e1a",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Signature: a.sort(*, key=None, reverse=False)\n",
    "# Docstring:\n",
    "# Sort the list in ascending order and return None.\n",
    "#\n",
    "# The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n",
    "# order of two equal elements is maintained).\n",
    "#\n",
    "# If a key function is given, apply it once to each list item and sort them,\n",
    "# ascending or descending, according to their function values.\n",
    "#\n",
    "# The reverse flag can be set to sort in descending order.\n",
    "# Type:      builtin_function_or_method"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3efcccd",
   "metadata": {},
   "source": [
    "# 列表vs字符串\n",
    "\n",
    "列表是可变的（Mutable）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78973d3a",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a = [1, 2, 3, 4]\n",
    "a[0] = 100\n",
    "a.insert(3, 200)\n",
    "a  # [100, 2, 3, 200, 4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5c2dca6",
   "metadata": {},
   "source": [
    "字符串是不可变的（Immutable）:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5811d15a",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "s = \"hello world\"\n",
    "\n",
    "# 通过索引改变会报错\n",
    "s[0] = 'k'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c964f483",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "s.insert(3, 'm')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39884f8b",
   "metadata": {},
   "source": [
    "字符串方法只是返回一个新字符串，并不改变原来的值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09d4ef6a",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "print(s.replace('world', 'Mars'))  # hello Mars\n",
    "print(s)  # hello world\n",
    "\n",
    "# 如果想改变字符串的值，可以用重新赋值的方法：\n",
    "s = s.replace('world', 'YunYun')\n",
    "print(s)  # hello YunYun"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5b6955e",
   "metadata": {},
   "source": [
    "### 面试题：\n",
    "1. 可变数据类型: list, dictionary, set, numpy array, user defined objects\n",
    "2. 不可变数据类型: integer, float, long, complex, string, tuple, frozenset，强调下 tuple 是不可变的\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bcaba31",
   "metadata": {},
   "source": [
    "本节完。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b9445e4",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}